﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace CNKI.TPI.Web.Base.HtmlHelp
{
    public static class CheckBoxExtensions
    {
        public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper,
                                                        Expression<Func<TModel, string>> expression, string trueValue)
        {

            if (expression == null)
            {

                throw new ArgumentNullException("expression");

            }

            ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, string>(expression,
                                                                                             htmlHelper.ViewData);

            bool isChecked = false;

            if (modelMetadata.Model != null && modelMetadata.Model.ToString().Trim() == trueValue)
            {

                isChecked = true;

            }

            string modelName = ExpressionHelper.GetExpressionText(expression);

            return htmlHelper.CheckBox(modelName, isChecked);

        }

    }
}